<template>
  <BasicModal
    :title="getTitle"
    :width="400"
    :canFullscreen="false"
    :centered="true"
    :destroyOnClose="true"
    @register="registerModal"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" :model="model" />
  </BasicModal>
</template>

<script lang="ts" name="BankModal" setup>
import { ref, computed, unref, reactive } from "vue";
import { BasicModal, useModalInner } from "/@/components/Modal";
import { BasicForm, useForm } from "/@/components/Form/index";
import { useMessage } from "/@/hooks/web/useMessage";
import { list, queryById, save } from "/@/views/weixin/menu/menu.api";

const $message = useMessage();

// 声明Emits
const emit = defineEmits(['success', 'register']);

const labelCol = { span: 6 }
const wrapperCol = { span: 16 }
//表单modal
const model = reactive<any>({})

//表单配置
const [registerForm, {resetFields, setFieldsValue, updateSchema, validate}] = useForm({
  schemas: [
    { label: "", field: "id", component: "Input", show: false },
    { label: "菜单名称", field: "name", component: "Input", required: true },
    { label: "上级菜单", field: "fatherid", component: 'TreeSelect', required: true,
      componentProps: {
        replaceFields: {
          title: 'name',
          key: 'id',
          value: 'id',
        },
        dropdownStyle: {
          maxHeight: '50vh',
        },
        getPopupContainer: (node) => node.parentNode,
      },
    },
    { label: "动作设置", field: "type", required: true, component: 'Select',
      defaultValue: 'click',
      componentProps: ({ formActionType }) => {
        return {
          options: [
            { label: '消息触发类', value: 'click' },
            { label: '网页链接类', value: 'view' },
          ],
          onChange: (value) => {
            const { updateSchema, clearValidate } = formActionType;
            //清除校验
            clearValidate();

            if (value == 'click') {
              updateSchema([ { field: 'url', required: false, show: false } ]);
            } else {
              updateSchema([ { field: 'url', required: true, show: true } ]);
            }
          },
        };
      },
    },
    { label: "URL", field: "url", component: "Input", show: false, componentProps: { placeholder: "填写url，格式需要以http开头" } },
    { label: "消息类型", field: "msgtype", component: "Input", required: true,
      componentProps: ({ formActionType }) => {
        return {
          options: [
            { label: '文本', value: 'text' },
            { label: '图文', value: 'news' },
            { label: '扩展', value: 'expand' },
          ],
        }
      }
    },
    { label: "菜单标识", field: "menukey", component: "Input", required: true },
    { label: "排序", field: "sort", component: "InputNumber", componentProps: { placeholder: " " } }
  ],
  labelWidth: 90,
  baseColProps: {span: 22},
  showActionButtonGroup: false,
});

const formEl = ref();
const isUpdate = ref(true);
//表单赋值
const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
  setModalProps({confirmLoading: false});
  isUpdate.value = !!data?.isUpdate;
  await resetFields()
  let record = {}
  if (isUpdate.value) {
    record = await queryById({id: data?.record?.id});
  }

  //获取下拉树信息
  const treeData = await list();
  updateSchema([
    {
      field: 'fatherid',
      componentProps: { treeData },
    },
  ]);

  await setFieldsValue(record)
});
//标题
const getTitle = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));

//表单提交事件
async function handleSubmit() {
  try {
    setModalProps({confirmLoading: true});
    const values = await validate();
    await save(values).then((res) => {
      if (res.success) {
        $message.createMessage.success(res.message);
        //关闭弹窗
        closeModal();
        emit("success")
      } else {
        $message.createMessage.warning(res.message);
      }
    });
  } catch (e) {
    console.log(e);
  } finally {
    setModalProps({confirmLoading: false});
  }
}

</script>

<style lang="less" scoped>
:deep(.ant-upload.ant-upload-select-picture-card) {
  width: 360px!important
}
</style>
